package heman.server.controllers;



import heman.server.database.DatabaseController;
import heman.server.entities.Player;
import heman.server.entities.PlayerManager;
import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.framework.debug.Debug;
import ks.server.interfaces.IProcessServerMessage;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * 
 *  Controller to handle ban. 
 *  
 *  <request version="1.0" id="589a39591271844e3fbe32bbb9281ad9">
 *  <ban player="129382" allow="false"/>
 *  </request>
 *  
 *  A player cannot unregister himself when he is on a table.
 *  He must leave that table before unregister. This should be constrained by
 *  the client's GUI.
 *  
 *  When the player is unregister, it will be forced to logout.
 *  
 *    @author Wenlong Liu
 *  */

public class UnregisterController implements IProcessServerMessage {

	public boolean process(Communicator com, Message m) {
		String originator=m.getOriginator();
		
       
        Node root = m.contents();
        NamedNodeMap map=root.getAttributes();

        Node psNode=map.
        		getNamedItem("password");
        
        if (psNode ==null) return false;
        String ps=psNode.getNodeValue();
        
        if (!DatabaseController.getPsw(originator).equals(ps)) return false;
  
        DatabaseController.unRegister(originator);
        
        //Construct a logout message
        StringBuilder sBuilder=new StringBuilder();
        sBuilder.append("<request version=\"1.0\" id=\"");
        sBuilder.append(m.id);
        sBuilder.append("\"><logout /> </request>");
        Document dd = Message.construct(sBuilder.toString());
		
		Message tm=new Message(dd);
		tm.setOriginator(originator);
        

        //logout
        return new LogoutController().process(com, tm);

        
	}
}